package com.ruomeng.robot.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import com.ruomeng.robot.entity.DefaultResponseObject;
import com.ruomeng.robot.entity.ExplosiveGoods;
import com.ruomeng.robot.entity.GlobalErrorCode;
import com.ruomeng.robot.entity.ResponseFail;
import com.ruomeng.robot.entity.ResponseObject;
import com.ruomeng.robot.entity.Tgroup;
import com.ruomeng.robot.entity.TgroupUpdateVO;
import com.ruomeng.robot.service.GroupService;
import com.ruomeng.robot.util.SysStringUtils;

import redis.clients.jedis.Jedis;

@RestController
@RequestMapping(value = {"/gmk/gatherShow"})
public class IndexController{
	
	@Autowired
	private GroupService groupService;
	@Autowired
	 private RedisTemplate redisTemplate;
	
	
	/**
	 * 
	 * @param type大的分类，热门采集群、综合、实时上新……
	 * @return
	 */
	@RequestMapping("/groupList")
	@ResponseBody
	public String groupList()
	{
		//获取群列表
		List<Tgroup> groupList=groupService.groupList(new Tgroup());
		int counts = groupService.showCount(new TgroupUpdateVO());
		Map<String, Object> map =new HashMap();
		map.put("groupList", groupList);//群列表
		map.put("counts", counts);//群列表
		return success(map);
	}
	/**
	 * 热门采集
	 * @param 
	 * @return
	 */
	@RequestMapping("/listShow")
	@ResponseBody
	public String listShow(TgroupUpdateVO group)
	{
		String str="";
	/*	String str = jedisUtil.get(RedisStatus.HOTCAIJI+group.getGroupIds()+group.getGoodSort()+group.getPageSize());
		if(SysStringUtils.isNotEmpty(str))
		{
			return str;
		}*/
		group.setPageSize((group.getPageSize()-1)*40);
		List<TgroupUpdateVO> list = groupService.listShow(group);
		int counts = groupService.showCount(group);
		Map<String, Object> map = new HashMap();
		map.put("list", list);
		map.put("pageCount", (int)Math.ceil((double)counts/40));
		str=success(map);
		/*if(list!=null && list.size()>0)
		{
			jedisUtil.set(RedisStatus.HOTCAIJI+group.getGroupIds()+group.getGoodSort()+group.getPageSize(), str);
			jedisUtil.expire(RedisStatus.HOTCAIJI+group.getGroupIds()+group.getGoodSort()+group.getPageSize(), 120);//120秒过期
		}*/
		System.out.println(str);
		return str;
	}
	
	/**
	 * type大的分类，热门采集群、综合、实时上新……
	 * @param 综合，实时上新，销量榜，领券榜，品质好货
	 * @return
	 */
	@RequestMapping("/showSynthesis")
	@ResponseBody
	public String showSynthesis(TgroupUpdateVO group)
	{
		
		group.setPageSize((group.getPageSize()-1)*40);
		if(group!=null && group.getType()!=null)
		{
			
			
			String str = "";
			/*String str = jedisUtil.get(RedisStatus.COMPOSITECAIJI+group.getType()+group.getGoodSort()+group.getPageSize()+group.getFlag()+group.getGoodTimeSort());

			if(SysStringUtils.isNotEmpty(str))
			{
				
				return str;
			}*/
			if (group.getType()==1||group.getType()==2||group.getType()==6||group.getType()==5||group.getType()==7) {
				List<TgroupUpdateVO> list = groupService.zongheList(group);
				
			
				int	 counts = groupService.zongheListCount(group);
				Map<String, Object> map = new HashMap();
				map.put("list", list);
				map.put("counts", counts);
				map.put("pageCount", (int)Math.ceil((double)counts/40));
				str = success(map);
			}
			else {
				List<TgroupUpdateVO> list = groupService.showAll(group);
				int counts;
				if (group.getType()==3) {
					 counts = 100;
				}
				else {
					 counts = groupService.showTool(group);
				
				}
			
				Map<String, Object> map = new HashMap();
				map.put("list", list);
				map.put("counts", counts);
				map.put("pageCount", (int)Math.ceil((double)counts/40));
				str = success(map);
			}
			
			
			/*if(list!=null && list.size()>0)
			{
				jedisUtil.set(RedisStatus.COMPOSITECAIJI+group.getType()+group.getGoodSort()+group.getPageSize()+group.getFlag()+group.getGoodTimeSort(), str);
				jedisUtil.expire(RedisStatus.COMPOSITECAIJI+group.getType()+group.getGoodSort()+group.getPageSize()+group.getFlag()+group.getGoodTimeSort(), 120);//120秒过期
			}*/
			return str;
		}
		else
		{
			return error("类型不能为空！");
		}
		
	}
	
	
	@RequestMapping("/goodGoods")
	@ResponseBody
	public String goodGoods(TgroupUpdateVO group)
	{
		group.setPageSize((group.getPageSize()-1)*40);
		if(group!=null && group.getType()!=null)
		{
			
	
			String str = "";
			/*String str = jedisUtil.get(RedisStatus.COMPOSITECAIJI+group.getType()+group.getGoodSort());
			if(SysStringUtils.isNotEmpty(str))
			{
				
				return str;
			}*/
			
			List<TgroupUpdateVO> list = groupService.zongheList(group);
			
			
			int	 counts = groupService.zongheListCount(group);
			Map<String, Object> map = new HashMap();
			map.put("list", list);
			map.put("counts", counts);
			map.put("pageCount", (int)Math.ceil((double)counts/40));
			str = success(map);
			/*if(list!=null && list.size()>0)
			{
				jedisUtil.set(RedisStatus.COMPOSITECAIJI+group.getType()+group.getGoodSort()+group.getPageSize()+group.getFlag()+group.getGoodTimeSort(), str);
				jedisUtil.expire(RedisStatus.COMPOSITECAIJI+group.getType()+group.getGoodSort()+group.getPageSize()+group.getFlag()+group.getGoodTimeSort(), 120);//120秒过期
			}*/
		
			return str;
		}
		else
		{
			return error("类型不能为空！");
		}
		
	}
	
	
	/**
	 * @return搜索不能走缓存
	 */
	@RequestMapping("/showSearch")
	@ResponseBody
	public String showSearch(TgroupUpdateVO group)
	{
		group.setPageSize((group.getPageSize()-1)*40);
		if(group!=null && group.getType()!=null)
		{
			group.setSearch(1);
			List<TgroupUpdateVO> list = groupService.searchAll(group);
			int counts = groupService.searchTool(group);
			Map<String, Object> map = new HashMap();
			map.put("list", list);
			map.put("counts", counts);
			map.put("pageCount", (int)Math.ceil((double)counts/40));
			return success(map);
		}
		else
		{
			return error("类型不能为空！");
		}
	}
	/**
	 * 
	 * @param date时间，如果是null就是今天
	 * @param type 排序规则：3-录入时间   2-领券量
	 * @param page null就是第一页，每页40条数据
	 * @return
	 */
	@RequestMapping(value="/baokuan")
	@ResponseBody
	public String listView(HttpServletRequest request,ExplosiveGoods goods,String date,Integer type,Integer page){
		
		/*System.out.println("22222222222222222");
		groupService.baokuanList(eGoods);*/
	    System.out.println(goods.getGoodTimeSort());
		String dateNowStr=null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		if(StringUtils.isNotEmpty(date))
		{
			dateNowStr=date;
		}
		else
		{
			dateNowStr = sdf.format(new Date());
		}
		if(page==null || (page!=null && page == 1))
		{
			page=0;
		}
		
		if(type==null)
		{
			type=2;
		}

      //  String str=jedisUtil.get("FADDISH"+dateNowStr+type+page);
		String str=null;
		if(SysStringUtils.isNotEmpty(str))
		{
			return str;
		}
		
		
		goods.setEntryTime(dateNowStr);
		goods.setShopType(type);
		if(page==null || (page!=null && page == 1 || page == 0))
		{
			goods.setPageSize(0);
		}
		else
		{
			goods.setPageSize((page-1)*40);
		}
		int sumPage=groupService.baokuanListCount(goods);
		List<ExplosiveGoods> list = groupService.baokuanList(goods);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", list);
		map.put("pageCount", (int)Math.ceil((double)80/40));
		map.put("counts", sumPage);
		map.put("pageCount", (int)Math.ceil((double)sumPage/40));
		str = success(map);

		/*if(list!=null && list.size()>0)
		{
			jedisUtil.set("FADDISH"+dateNowStr+type+page, str);
			if(StringUtils.isEmpty(date) || sdf.format(new Date()).equals(date))
			{
				jedisUtil.expire("FADDISH"+dateNowStr+type+page, 120);//120秒过期
			}
		}*/
		
		return str;

		
	}
	
	/**
	 * 成功
	 * @param 
	 * @author Sven
	 * @date 2016年11月30日14:50:03
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String success(Object obj) {
        ResponseObject result = new ResponseObject();    
        result.setData(obj);
	    String  str = JSON.toJSONString(result);
	    return  str;	
	}
	
	/**
	 * 错误
	 * @param 
	 * @author Sven
	 * @date 2016年11月30日14:50:03
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String error(String moreInfo) {
        ResponseFail result = new ResponseFail();  
        result.setMoreInfo(moreInfo);
	    String  str = JSON.toJSONString(result);
	    return  str;	
	}
	

}
